function opt_debt_maturity_plot(b, fixedcost, N)

%%% b = 2.4, fixedcost = 0.4, N = 6 are used for the example in the paper

struc_input = diverse_struc_input(0.05, N);

[~, equity_mv] = debtoverhang_fun(struc_input, 'diverse');

debt_mv = struc_input.single_asset - equity_mv;
Strike = struc_input.Strike;

rangetocheck = [0.5, 5.5];

close all

assumedcr = 0.1;
npoinnts = 200;
xvec = linspace(rangetocheck(1), rangetocheck(2), npoinnts)';
xvec_actual = 0.1835*xvec.^2 + 0.674*xvec + 0.3816;
cost_vec = (1 + exp(-b*xvec))*fixedcost;
debtoverhangvec = nan(npoinnts,1);
debtoverhangvec_single = nan(npoinnts,1);
debtoverhangvec_single_test = nan(npoinnts,1);
StockSpec_single = stockspec(struc_input.StockSpec.Sigma(1), sum(struc_input.StockSpec.AssetPrice));
struc_input_single = struc_input;
struc_input_single.StockSpec = StockSpec_single;
equity_mv_vec = nan(npoinnts,1);
xvec_single = nan(npoinnts,1);
parfor i = 1:length(xvec)
    [debtoverhangvec(i), ~, equity_mv_vec(i)] = debtoverhang_given_debtmat(struc_input, 'diverse', xvec(i));
    debtoverhangvec_single_test(i) = debtoverhang_given_debtmat(struc_input_single, 'single', xvec(i));
    [xvec_single(i), ~, debtoverhangvec_single(i)] = debtmat_given_equity(equity_mv_vec(i), 'single', struc_input_single, false);
end

adj_npv_vec = 1 - debtoverhangvec - cost_vec;
adj_npv_vec_single = 1 - debtoverhangvec_single - cost_vec;

adj_npv_max_vec = nan(size(adj_npv_vec));
adj_npv_max_vec_single = nan(size(adj_npv_vec));

[maxnpv, maxindex] = max(adj_npv_vec);
[maxnpv_single, maxindex_single] = max(adj_npv_vec_single);

adj_npv_max_vec(maxindex) = adj_npv_vec(maxindex);
adj_npv_max_vec_single(maxindex_single) = adj_npv_vec_single(maxindex_single);


close all
figure
yyaxis left
set(gcf, 'Position', [50 90 1450 1400]*0.5)
set(gcf, 'Color', 'w');
hleft = zeros(1,4);
hold on
hleft(1) = plot(xvec, adj_npv_vec, 'Color', 'b', 'LineWidth', 2, 'LineStyle', '-', 'DisplayName', ['Multi-division (' num2str(N) ') NPV (left Y axis)']);
hleft(2) = plot(xvec_single, adj_npv_vec_single, 'Color', 'r', 'LineWidth', 2, 'LineStyle', ':', 'DisplayName','Stand-alone NPV (left Y axis)');
hleft(3) = plot(xvec, adj_npv_max_vec, 'ob', 'MarkerSize', 8);
hleft(4) = plot(xvec_single, adj_npv_max_vec_single, 'or', 'MarkerSize', 8);
hold off
lincolor = 'b';
lincolor_single = 'r';

xlin(xvec(maxindex), 'Optimal debt maturity for multi-division', 0,  maxnpv, maxnpv/3, lincolor, 'top');
xlin(xvec_single(maxindex_single), 'Optimal debt maturity for stand-alone', 0,  maxnpv_single, maxnpv_single/3, lincolor_single, 'bottom');
% ylim([0, 0.6])
ylabel('Overhang and cost adjusted NPV (in unit of $\varepsilon$)', 'Interpreter','latex')

yyaxis right

hold on
hright = zeros(1,4);
hright(1) = plot(xvec, equity_mv_vec, 'Color', 'b', 'LineWidth', 2, 'LineStyle', '--', 'DisplayName', ['Multi-division (' num2str(N) ') equity (right Y axis)']);
hright(2) = plot(xvec_single, equity_mv_vec, '-o',  'Color', 'r', 'LineWidth', 2, 'MarkerIndices',1:round(npoinnts/8):length(equity_mv_vec), 'markersize', 5, 'DisplayName','Stand-alone equity (right Y axis)');
hold off
axis tight
ylabel('Market value of equity', 'Interpreter','latex')

legend([hleft(1:2)'; hright(1:2)'], 'Interpreter','latex', 'Location', 'SouthEast', 'NumColumns',1, 'Box', 'off', 'Interpreter','latex');

xlabel('Debt maturity (years)', 'Interpreter','latex')
grid minor